package com.caiyi.financial.nirvana.sms.scheduled;

import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.util.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class HeartbeatTask {

    private static final Logger logger = LoggerFactory.getLogger(HeartbeatTask.class);
    private static final String TEST_MESSAGE = "test message !!! ";

    /**
     * 发布订阅心跳机制5分钟一次
     */
    @Scheduled(fixedRate = Constant.TEN_MINUTES_OF_SECOND * 500, initialDelay = Constant.TEN_MINUTES_OF_SECOND * 500)
    public void heartbeat() {
        try {
            long count = JedisService.publish(JedisKey.SMS_SUBSCRIBE_GATEWAY_ACTION, TEST_MESSAGE);
            logger.info("HeartbeatThread publish key {}, value : {}, return : {}", JedisKey.SMS_SUBSCRIBE_GATEWAY_ACTION, TEST_MESSAGE, count);
            count = JedisService.publish(JedisKey.SMS_SUBSCRIBE_API_ACTION, TEST_MESSAGE);
            logger.info("HeartbeatThread publish key {}, value : {}, return : {}", JedisKey.SMS_SUBSCRIBE_API_ACTION, TEST_MESSAGE, count);
        } catch (Exception e) {
            logger.error("HeartbeatTask heartbeat error ", e);
        }
    }

}
